public class MaxConsecutiveOnes {
    // 最大连续1的个数III https://leetcode.cn/problems/max-consecutive-ones-iii/description/
    public int longestOnes(int[] nums, int k) {
        int len=0;
        int zero=0;
        int left=0,right=0;
        for(;right<nums.length;right++) {
            if(nums[right]==0) zero++;
            while(zero>k) {
                len=Math.max(len,right-left);
                if(nums[left]==0) {
                    zero--;
                }
                left++;
            }
        }
        len=Math.max(len,right-left);
        return len;
    }
}
